var pageSettings;
function bodyDoOnload(){
	initVariables();
	loadTree();
}


/**
 * sets the active record id
 * @param 		int		recordId
 */
function setRecordId(recordId){
	pageSettings.saveInfo.moduleInfo.recordId = recordId;
}


/**
 * returns the active record id
 * @return		int		recordId
 */
function getRecordId(){
	return pageSettings.saveInfo.moduleInfo.recordId;
}


/**
 * function handles the new action
 * checks the modulInfo and calls the particular function for handling new
 */
function handleNew(){
	var moduleName = pageSettings.saveInfo.moduleInfo.name;
	setRecordId(null);
	if (moduleName != null){
		switch(moduleName){
		case 'NEWS':
			handleNewNews();
			break;
		case 'GALLERY':
			handleNewGalery();
			break;
		}
	}
}


/**
 * handles editing a record
 * @param 		int		recordId
 */
function handleEdit(recordId){
	setRecordId(recordId);
	var moduleName = pageSettings.saveInfo.moduleInfo.name;
	if (moduleName != null){
		switch(moduleName){
		case 'NEWS':
			editNews(recordId);
			break;
		case 'GALLERY':
			editGallery(recordId);
			break;
		}
	} else setStatus('Keine Daten gefunden', true);
}


/**
 * function handles the save action
 * checks the modulInfo and calls the particular save function
 * @param		int		recordId
 */
function handleSave(){
	var moduleName = pageSettings.saveInfo.moduleInfo.name;
	if (moduleName != null){
		switch(moduleName){
		case 'CMS':
			saveHtmlFile();
			break;
		case 'NEWS':
			saveNews(getRecordId());
			break;
		case 'GALLERY':
			saveGallery(getRecordId());
			break;
		case 'IMAGE':
			saveImage(getRecordId());
			break;
		}
	} else setStatus('Keine Daten gefunden', true);
}


/**
 * initializes all needed variables
 */
function initVariables(){
	pageSettings = {};
	pageSettings.saveInfo = {};
	pageSettings.saveInfo.moduleInfo = {};
}


/**
 * loads menue tree
 */
function loadTree(){
	loadHtmlContent('menueContent', loadTreeUrl, null, function(){
		$('#menueContent').toggletree();
	});
}


/**
 * handles the logout for current user
 * sets authentication cookies to null and redirect do index.php
 */
function logout(){
	cookie('EMAIL', null);
	cookie('PASSWORD', null);
	document.location.href = indexUrl + '?' + getEncodedParams({'T':'ADMIN'});
}


/**
 * loads the content from given script 
 * shows an loading animation while loading
 * @param 		string		target		id of div
 * @param 		string		scriptUrl
 * @param 		array		params
 * @param 		function	callback
 */
function loadHtmlContent(target, scriptUrl, params, callback){
	var target = '#' + target;
	$(target).html('<img src="./admin/images/loading.gif" alt="loading..." />');
	$.ajax({
		url: scriptUrl,
		type: 'POST',
		dataType: 'html',
		data: params,
		success: function(response){
		$(target).html(response);
		if (typeof(callback) == 'function')
			callback();
	}
	});
}


/**
 * shows status message in status bar
 * @param 		string		message
 * @param 		bool		isError
 * @return
 */
function setStatus(message, isError){
	var content = '';
	if (isError)
		content = '<span style="color: #BA0303; font-weight:bold;">' + message + '</span>';
	else
		content = '<span style="color: #147A0A; font-weight:bold;">' + message + '</span>';

	$('#statusbar').html(content);
}



